package com.liruo.treasureattic.service.user.service.impl;

import com.liruo.treasureattic.common.httpmsg.exception.CommonResultException;
import com.liruo.treasureattic.common.httpmsg.result.Errno;
import com.liruo.treasureattic.common.model.entity.User;
import com.liruo.treasureattic.common.model.vo.user.UserVo;
import com.liruo.treasureattic.common.util.ValidatorUtil;
import com.liruo.treasureattic.config.common.util.SessionHolder;
import com.liruo.treasureattic.service.user.mapper.FollowMapper;
import com.liruo.treasureattic.service.user.service.FollowService;
import com.liruo.treasureattic.service.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:liruo
 * @Date:2023-02-27-23:29:23
 * @Desc
 */
@Service
public class FollowServiceImpl implements FollowService{
    @Resource
    private FollowMapper followMapper;
    @Resource
    private UserService userService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean follow(Long followId) {
        ValidatorUtil.requireGreat0(followId, followId);
        UserVo loginUser = followCheck(followId);
        Long followerId = loginUser.getId();

        if(followMapper.isFollow(followerId, followId)){
            throw new CommonResultException(Errno.PARAMS, "has follow the user");
        }

        userService.follow(followerId, followId, true);
        return followMapper.follow(followerId, followId);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean unfollow(Long followId) {
        ValidatorUtil.requireGreat0(followId);
        UserVo loginUser = followCheck(followId);
        Long followerId = loginUser.getId();

        if(!followMapper.isFollow(followerId, followId)){
            throw new CommonResultException(Errno.PARAMS, "has not follow the user");
        }

        userService.follow(followerId, followId, false);
        return followMapper.unfollow(followerId, followId);
    }


    /**
     *
     * @param followId !=null且user登录了，请求following-user存在
     * @return
     */
    private UserVo followCheck(Long followId) {
        if(followId == null){
            throw new CommonResultException(Errno.PARAMS);
        }
        //登录
        UserVo loginUser = SessionHolder.getUser();

        //user存在
        if(!userService.existsById(followId)){
            throw new CommonResultException(Errno.PARAMS, "the user not exists");
        }

        return loginUser;
    }

    @Override
    public List<UserVo> getFollowings() {
        //登录
        UserVo loginUser = SessionHolder.getUser();

        List<Long> followings = followMapper.getFollowings(loginUser.getId());
        List<User> users = userService.listByIds(followings);
        return users.stream().map(user->{
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(user, userVo);
            return userVo;
        }).toList();
    }

    @Override
    public List<UserVo> getFollowers() {
        //登录
        UserVo loginUser = SessionHolder.getUser();

        List<Long> followers = followMapper.getFollowers(loginUser.getId());
        List<User> users = userService.listByIds(followers);
        return users.stream().map(user->{
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(user, userVo);
            return userVo;
        }).toList();
    }
}
